<?php
namespace Home\Controller;
use Think\Controller;
use Home\Biz\StringBiz;
use Home\Biz\CommonBiz;
use Home\Biz\PromoterBiz;


class PromoterController extends LoginBaseController {	
	/**
	 * 推广人列表显示
	 */
	public function promoterList(){
		$this->assign('module_id',	'promoter');
		$this->assign('page_id',	'promoterList');
		$this->assign('page_name',	'推广人信息列表');
		
		$this->display('promoter/list');
    }
    
    
    /**
     * 推广人详情页面
     */
    public function detail(){
    	$promoter_id = I('promoter_id');
    	
    	$promoter_en = M('promoterInfo')->where("promoter_id='%s'", $promoter_id)->find();
    	$promoter_en['status_text']	= StringBiz::getPromoterInfoStatusText($promoter_en['status']);
    	
    	$promoter_en['id_card_image_list']		= StringBiz::getImageFullUrlList($promoter_en['id_card_image_list'], 1);
    	$promoter_en['business_license_image']	= StringBiz::getImageFullUrl($promoter_en['business_license_image'], 1);
    	$promoter_en['organization_image']		= StringBiz::getImageFullUrl($promoter_en['organization_image'], 1);
    	$promoter_en['tax_registration_image']	= StringBiz::getImageFullUrl($promoter_en['tax_registration_image'], 1);
    	
    	$this->assign('promoter_en', $promoter_en);
    	
    	
    	$this->assign('module_id',	'promoter');
    	$this->assign('page_id',	'promoterList');
    	$this->assign('page_name',	'推广人信息');
    	$this->display('promoter/detail');
    }
    
    public function amountList(){
    	$promoter_id = I('promoter_id');
    	$this->assign('promoter_id', $promoter_id);
    	$this->assign('module_id',	'promoter');
    	$this->assign('page_id',	'amountList');
    	$this->assign('page_name',	'推广人余额列表');
    	$this->display('promoter/amountList');
    }
    
    
    /**
     * 提现处理
     */
    public function withdrawList(){
    	$this->assign('module_id',	'promoter');
    	$this->assign('page_id',	'withdrawList');
    	
    	$this->assign('page_name',	'推广人提现处理');
    	$this->display('promoter/withdrawList');
    }
    
    public function showWithdrawList(){
    	$this->assign('module_id',	'promoter');
    	$this->assign('page_id',	'showWithdrawList');
    	 
    	$lst_promoter_withdraw_en = array();
    	$lst_withdraw_id_str= I('lst_withdraw_id');
    	$this->assign('lst_withdraw_id', $lst_withdraw_id_str);
    	
    	if(empty($lst_withdraw_id_str)){
    		$lst_promoter_withdraw_en	= M('promoterWithdraw')
    		->field("promoter_withdraw.*, promoter_info.amount,promoter_info.withdraw_amount AS promoter_withdraw_amount")
    		->join("promoter_info ON promoter_withdraw.promoter_id = promoter_info.promoter_id")
    		->where("promoter_withdraw.status=1")
    		->select();
    	}
    	else{
    		$lst_withdraw_id	= explode(',', $lst_withdraw_id_str);
    		$con_array			= array();
    		$con_array['id']	= array('in', $lst_withdraw_id);
            $con_array['promoter_withdraw.status']= array('eq', 1);

    		$lst_promoter_withdraw_en	= M('promoterWithdraw')
    			->field("promoter_withdraw.*, promoter_info.amount,promoter_info.withdraw_amount AS promoter_withdraw_amount")
    			->join("promoter_info ON promoter_withdraw.promoter_id = promoter_info.promoter_id")
    			->where($con_array)
    			->select();
    	}
    	
    	$all_withdraw_amount = 0;
    	foreach($lst_promoter_withdraw_en as $key => $promoter_withdraw_en){
    		$lst_promoter_withdraw_en[$key]['status_text'] = StringBiz::getWithdrawStatusText($promoter_withdraw_en['satus']);
    		$all_withdraw_amount	= $all_withdraw_amount + $promoter_withdraw_en['withdraw_amount'];
    	}
    	
    	
    	$this->assign('all_withdraw_amount', $all_withdraw_amount);
    	$this->assign('lst_promoter_withdraw_count', count($lst_promoter_withdraw_en));
    	$this->assign('lst_promoter_withdraw_en', $lst_promoter_withdraw_en);
    	
    	
    	$this->assign('page_name',	'申请提现列表');
    	$this->display('promoter/showWithdrawList');
    }
    
    /**
     * 支付宝批量请求
     */
    public function alipayBatchTransfer(){
    	header("Content-type:text/html;charset=utf-8");
    	
    	$lst_withdraw_id_str = I('lst_withdraw_id');
    	
    	$lst_promoter_withdraw_en = array();
    	
    	if(empty($lst_withdraw_id_str)){
    		$lst_promoter_withdraw_en	= M('promoterWithdraw')
    		->field("promoter_withdraw.*, promoter_info.alipay_id,promoter_info.alipay_name,promoter_info.amount,promoter_info.withdraw_amount AS promoter_withdraw_amount")
    		->join("promoter_info ON promoter_withdraw.promoter_id = promoter_info.promoter_id")
    		->where("promoter_withdraw.status=1")
    		->select();
    	}
    	else{
    		$lst_withdraw_id	= explode(',', $lst_withdraw_id_str);
    		$con_array			= array();
    		$con_array['id']	= array('in', $lst_withdraw_id);
    		$lst_promoter_withdraw_en	= M('promoterWithdraw')
    		->field("promoter_withdraw.*, promoter_info.alipay_id,promoter_info.alipay_name,promoter_info.amount,promoter_info.withdraw_amount AS promoter_withdraw_amount")
    		->join("promoter_info ON promoter_withdraw.promoter_id = promoter_info.promoter_id")
    		->where($con_array)
    		->select();
    	}
    	 
    	$lst_promoter_withdraw_count = count($lst_promoter_withdraw_en);
    	$all_withdraw_amount = 0;
    	foreach($lst_promoter_withdraw_en as $key => $promoter_withdraw_en){
    		$lst_promoter_withdraw_en[$key]['status_text'] = StringBiz::getWithdrawStatusText($promoter_withdraw_en['satus']);
    		$all_withdraw_amount	= $all_withdraw_amount + $promoter_withdraw_en['withdraw_amount'];
    	}
    	
    	
    	/**************************请求参数**************************/
    	//服务器异步通知页面路径
    	$notify_url = C('AlipayBatchConfig.notify_url');
    	//需http://格式的完整路径，不允许加?id=123这类自定义参数
    	
    	//付款账号
    	$email = C('AlipayBatchConfig.email');
    	//必填
    	
    	//付款账户名
    	$account_name = C('AlipayBatchConfig.account_name');
    	//必填，个人支付宝账号是真实姓名公司支付宝账号是公司名称
    	
    	//付款当天日期
    	$pay_date = date("Y-m-d", time());
    	//必填，格式：年[4位]月[2位]日[2位]，如：20100801
    	
    	//批次号
    	$batch_no = date("Ymd", time()).CommonBiz::getAlipayBatchNo();
    	//必填，格式：当天日期[8位]+序列号[3至16位]，如：201008010000001
    	
    	//付款总金额
    	$batch_fee = $all_withdraw_amount;
    	//必填，即参数detail_data的值中所有金额的总和
    	
    	//付款笔数
    	$batch_num = $lst_promoter_withdraw_count;
    	//必填，即参数detail_data的值中，“|”字符出现的数量加1，最大支持1000笔（即“|”字符出现的数量999个）
    	
    	//付款详细数据
    	$detail_data = PromoterBiz::generateWithdrawDetailText($lst_promoter_withdraw_en);
    	//必填，格式：流水号1^收款方帐号1^真实姓名^付款金额1^备注说明1|流水号2^收款方帐号2^真实姓名^付款金额2^备注说明2....
    	/************************************************************/
    	
    	$alipay_config = array();
    	$alipay_config = C('AlipayBatchConfig');
    	
    	//构造要请求的参数数组，无需改动
    	$parameter = array(
    			"service" => "batch_trans_notify",
    			"partner" => trim($alipay_config['partner']),
    			"notify_url"	=> $notify_url,
    			"email"	=> $email,
    			"account_name"	=> $account_name,
    			"pay_date"	=> $pay_date,
    			"batch_no"	=> $batch_no,
    			"batch_fee"	=> $batch_fee,
    			"batch_num"	=> $batch_num,
    			"detail_data"	=> $detail_data,
    			"_input_charset"	=> trim(strtolower($alipay_config['input_charset']))
    	);
    	
    	//建立请求
    	import('Vendor.alipay.alipay_submit');
    	$alipaySubmit = new \AlipaySubmit($alipay_config);
    	
    	$html_text = $alipaySubmit->buildRequestForm($parameter,"get", "确认");
//     	echo json_encode($html_text);
    	echo $html_text;
    }
    
    
    
}